-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Create 0071-simplify-path.js #2614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Solved simplify-path in JS.
javascript/0071-simplify-path.js
Outdated
var simplifyPath = function(path) { | ||
let currunt = ''; | ||
let myStack = []; | ||
path = '/' + path + '/'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Use interpolation
path = `/${path}/`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually. We can just remove that third if statement. if the stack is empty then nothing would happen. We don't even need to check it. I updated the code please have a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aadil42 This can be simplified
/**
* Stack
* Time O(N) | Space O(N)
* https://leetcode.com/problems/simplify-path
* @param {string} path
* @return {string}
*/
var simplifyPath = (path, slash = '/', stack = []) => {
const paths = path.split(slash).filter(Boolean);
for (const _path of paths) traversePath(_path, stack);
return `${slash}${stack.join(slash)}`;
};
const traversePath = (path, stack) => {
if (canPush(path)) return stack.push(path);
if (canPop(path, stack)) stack.pop();
};
const canPush = (path) => !(
isCurrentDirectory(path) ||
isParentDirectory(path)
);
const canPop = (path, stack) =>
isParentDirectory(path) &&
!isEmpty(stack);
const isCurrentDirectory = (path) => (path === '.');
const isParentDirectory = (path) => (path === '..');
const isEmpty = ({ length }) => (0 === length);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, it's more readable. It's intimidating at first but if you try to read the code it's quite better than mine. Nicely Modularized. I have updated the file. Please have a look.
javascript/0071-simplify-path.js
Outdated
for(let i = 0; i < path.length; i++) { | ||
|
||
console.log(myStack); | ||
if(path[i] === '/') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Avoid nesting more than 3 times
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any other way. Can you kindly show me How can I avoid nested if statements and still get the logic right? Thank you :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use continue;
when you need to exit early to avoid else
using backticks
Removed unnecessary nested if statment
Modularizing the code.
Solved simplify-path in JS.
File(s) Added: 0071-simplify-path.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/simplify-path/submissions/986179327/